﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SA2012TBRace
{
	class MemoryCacheDB
	{
		private readonly string _CacheDatabaseLocation;
		private readonly string _ReportGeocacheNotFoundFileNameAndLocation;
		private Dictionary<string, CacheInfo> _Caches;
		private static List<string> _GeocachesNotFound = new List<string>();
        private int cacheNotFoundCounter = 0;
        private int cacheNotFoundCounterMax = 29;

		public MemoryCacheDB(string cacheDatabaseLocation, string reportGeocacheNotFoundFileNameAndLocation)
		{
			_CacheDatabaseLocation = cacheDatabaseLocation;
			_ReportGeocacheNotFoundFileNameAndLocation = reportGeocacheNotFoundFileNameAndLocation;
			_Caches = new Dictionary<string, CacheInfo>();
		}

		internal bool IsCitoCache(string gcCode)
		{
			if (CheckCacheType(gcCode, "C")) return true;

			return false;
		}

        internal bool IsEventCache(string gcCode)
        {
            if (CheckCacheType(gcCode, "E")) return true;

            return false;
        }

		private void ReportCacheNotInDatabaseError(string gcCode)
		{
            cacheNotFoundCounter++;
			if (!_GeocachesNotFound.Contains(gcCode))
			{
				_GeocachesNotFound.Add(gcCode);

				using (var writer = File.AppendText(_ReportGeocacheNotFoundFileNameAndLocation))
				{
                    writer.Write(gcCode + ",");
                    if (cacheNotFoundCounter == cacheNotFoundCounterMax)
                    {
                        writer.Write("\r\n");
                        cacheNotFoundCounter = 0;
                    }
				}
			}
		}

		internal bool IsMegaCache(string gcCode)
		{
			if (CheckCacheType(gcCode, "Z")) return true;

			return false;
		}


        internal bool IsFebruaryQualifyingCache(string gcCode)
        {
            if (!_Caches.ContainsKey(gcCode))
            {
                var cacheInfo = SQLiteDatabase.GetCacheDetails(gcCode, _CacheDatabaseLocation);

                if (cacheInfo != null)
                {
                    _Caches.Add(gcCode, cacheInfo);
                }
                else
                {
                    ReportCacheNotInDatabaseError(gcCode);
                }
            }

            if (_Caches.ContainsKey(gcCode))
                if (
                        (
                            _Caches[gcCode].Name.Contains("Love")
                            ||
                            _Caches[gcCode].Name.Contains("Red")
                            ||
                            _Caches[gcCode].Name.Contains("Hotel")
                            ||
                            _Caches[gcCode].Name.Contains("February")
                            ||
                            _Caches[gcCode].Name.Contains("Valentine")
                            ||
                            _Caches[gcCode].Name.Contains("Heart")
                            ||
                            _Caches[gcCode].Name.Contains("Cupid")
                            ||
                            _Caches[gcCode].Name.Contains("Honey")
                            ||
                            _Caches[gcCode].Name.Contains("Angel")
                            ||
                            _Caches[gcCode].Name.Contains("Chocolate")
                        )
                    )
                    return true;

            return false;
        }


		internal bool IsTBHotelCache(string gcCode)
		{
			if (!_Caches.ContainsKey(gcCode))
			{
				var cacheInfo = SQLiteDatabase.GetCacheDetails(gcCode, _CacheDatabaseLocation);

				if (cacheInfo != null)
				{
					_Caches.Add(gcCode, cacheInfo);
				}
				else
				{
					ReportCacheNotInDatabaseError(gcCode);
				}
			}

			if (_Caches.ContainsKey(gcCode))
				if (
						(
							_Caches[gcCode].Name.Contains("Hotel") 
							|| 
							_Caches[gcCode].Name.Contains("Inn") 
							|| 
							_Caches[gcCode].Name.Contains("Lodge")
						)
						&&
						(
							_Caches[gcCode].Container == "Regular"
							||
							_Caches[gcCode].Container == "Large"
						)
					)
					return true;
			
			return false;
		}
 
		private bool CheckCacheType(string gcCode, string cacheTypeCode)
		{
			if (!_Caches.ContainsKey(gcCode))
			{
				var cacheInfo = SQLiteDatabase.GetCacheDetails(gcCode, _CacheDatabaseLocation);

				if (cacheInfo != null)
				{
					_Caches.Add(gcCode, cacheInfo);
				}
				else
				{
					ReportCacheNotInDatabaseError(gcCode);
				}
			}

			if (_Caches.ContainsKey(gcCode))
				if (_Caches[gcCode].CacheType == cacheTypeCode)
					return true;

			return false;
		}
	}
}
